﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using TSqlServer;

public partial class user_user_index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TUser1.CheckUser(this);
            if (!TUser1.CheckGrant("A01",Session["userid"].ToString(), this))
            {
                Response.Write("<font style=\"FONT-SIZE: 10pt; COLOR: red\">对不起，您没有获得该项功能的授权！</font>");
                Response.End();
            };
            
            btnInsert.Attributes.Add("OnClick", "javascript:return chk();");
            btnUpdate.Attributes.Add("OnClick", "javascript:return chk();");

            string sSql = "select grpcode,grpname from t_user_group where status=0 order by sortnum";
            TCommon.LoadComBox3(sSql, grpcode);

            Get_dlTable();
        }
    }

    private void Get_dlTable()
    {
        string sSql = "select userid,username,dbo.getgroupbycode(grpcode) as grpname,lvl,dbo.getstatusbyid(status) as status from t_user1 order by username";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        dlTable.DataSource = myReader.FDataReader;
        dlTable.DataBind();
        //关闭
        myReader.Free();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        TUser1 oUser = new TUser1();
        if (!oUser.FindKey("userid", txtuserid.Text.Trim()))
        {
            oUser.UserID = txtuserid.Text.Trim();
            oUser.PWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("1234", "MD5");
            oUser.UserName = txtusername.Text.Trim();
            oUser.GrpCode = grpcode.SelectedValue;
            oUser.Lvl = Convert.ToInt16(lvl.SelectedValue);
            if (isshow.Checked) { oUser.Status = 0; } else { oUser.Status = 1; };
            oUser.InMan = "admin";
            oUser.Insert();
            
            //新建对应的文件夹，用来保存上传的图片文件------------------
            string rootdir = "d:\\www\\easyncms\\uploadfiles\\";
            string rootdir2 = "d:\\www\\easyncms\\pictures\\";
            string rootdir3 = "d:\\www\\easyncms\\myfiles\\";
            string newfolder = rootdir + oUser.UserID;
            string newfolder2 = rootdir2 + oUser.UserID;
            string newfolder3 = rootdir3 + oUser.UserID;
            Directory.CreateDirectory(newfolder);
            Directory.CreateDirectory(newfolder2);
            Directory.CreateDirectory(newfolder2 + "\\small\\");
            Directory.CreateDirectory(newfolder2 + "\\small2\\");
            Directory.CreateDirectory(newfolder3);
            //---------------------------------------------------------------
            
            //更新列表
            msg.Text = "*";
            Get_dlTable();
        }
        else
        {
            msg.Text = "重复";
        }
    }
    protected void dlTable_ItemCommand(object source, DataListCommandEventArgs e)
    {
        LinkButton b = (LinkButton)e.CommandSource;
        switch (b.CommandName)
        {
            case "Modify":
                dlTable.SelectedIndex = e.Item.ItemIndex;
                txtuserid.Text = b.CommandArgument;
                TUser1 oUser = new TUser1(txtuserid.Text);
                oUser.Select();
                txtusername.Text = oUser.UserName;
                TCommon.SetCombox(oUser.GrpCode, grpcode);
                TCommon.SetCombox(oUser.Lvl.ToString(), lvl);
                if (oUser.Status == 0)
                {
                    isshow.Checked = true;
                }
                else
                {
                    isshow.Checked = false;
                }
                //---
                msg.Text = "只读";
                txtuserid.ReadOnly = true;
                btnInsert.Enabled = false;
                btnUpdate.Enabled = true;
                btnCancel.Enabled = true;
                break;
            case "Delete":
                if (b.CommandArgument != "admin")
                {
                    TUser1.DeleteByID(b.CommandArgument);
                    Get_dlTable();
                    txtuserid.Text = "";
                    txtusername.Text = "";
                    grpcode.SelectedIndex = 0;
                    lvl.SelectedIndex = 0;
                    msg.Text = "*";
                    txtuserid.ReadOnly = false;
                    btnInsert.Enabled = true;
                    btnUpdate.Enabled = false;
                    btnCancel.Enabled = false;
                    dlTable.SelectedIndex = -1;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "myscript2", "alert('admin用户不能删除！');", true);
                }
                break;
            case "Reset":
                TUser1.ResetPWD(b.CommandArgument);
                ClientScript.RegisterStartupScript(this.GetType(), "myscript1", "alert('初始完成！');", true);
                break;
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        TUser1 oUser = new TUser1(txtuserid.Text);
        oUser.UserName = txtusername.Text.Trim();
        oUser.GrpCode = grpcode.SelectedValue;
        oUser.Lvl = Convert.ToInt16(lvl.SelectedValue);
        if (isshow.Checked) { oUser.Status = 0; } else { oUser.Status = 1; };
        oUser.Update();

        txtuserid.Text = "";
        txtusername.Text = "";
        grpcode.SelectedIndex = 0;
        lvl.SelectedIndex = 0;
        isshow.Checked = true;

        msg.Text = "*";
        txtuserid.ReadOnly = false;
        btnInsert.Enabled = true;
        btnUpdate.Enabled = false;
        btnCancel.Enabled = false;
        dlTable.SelectedIndex = -1;

        Get_dlTable();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtuserid.Text = "";
        txtusername.Text = "";
        grpcode.SelectedIndex = 0;
        lvl.SelectedIndex = 0;
        isshow.Checked = true;

        msg.Text = "*";
        txtuserid.ReadOnly = false;
        btnInsert.Enabled = true;
        btnUpdate.Enabled = false;
        btnCancel.Enabled = false;
        dlTable.SelectedIndex = -1;
    }
}
